<?php
/**
 * PrimeController.php
 * Classe Controller des Primes
 */

namespace RHX\Controller;

use RHX\Model\Object\Prime;

/**
 * Classe Controller des Primes
 */
class PrimeController extends RHXController {

    /**
     * Action par défaut
     * 
     * @return array
     */
    public function indexAction() {
	// Tables
	$primeTbl = $this->getService('Table\PrimeTable');
        
	return array(
	    'prime'	 => 1,	    
	);
    }
    
     /**
     * Chargement de la liste des primes
     * 
     * @return array
     */
    
    public function loadIndexAction() {
        $this->layout('layout/ajax');
        
	// Tables
	$primeTbl = $this->getService('Table\PrimeTable');
        
        $primes = $primeTbl->getAll('id asc');
        
	return array(
	    'primes'	 => $primes,	    
	);
    }
    
     /**
     * MAJ des données string d'un prime 
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function updateStringDataAction() {
	// POST
	$post = $this->getRequest()
		->getPost();

	$pk	 = $post->pk;
	$name	 = $post->name;
	$value	 = addslashes($post->value);

	$sql	 = "UPDATE rh_prime SET $name = '$value' WHERE id = $pk";
	$adapter = $this->getAdapter();
	$result	 = $adapter->query($sql)->execute();

	$done = false;

	if ($result) {
	    $done = true;
	}

	return new \Zend\View\Model\JsonModel(array(
	    'done' => $done,
	));
    }
    
    /**
     * MAJ des données nombre (float/int) d'un prime 
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function updateNumberDataAction() {
	// POST
	$post = $this->getRequest()
		->getPost();

	$pk	 = $post->pk;
	$name	 = $post->name;
	$value	 = addslashes($post->value);

	$sql	 = "UPDATE rh_prime SET $name = $value WHERE id = $pk";
	$adapter = $this->getAdapter();
	$result	 = $adapter->query($sql)->execute();
        
        $done = false;

	if ($result) {
	    $done = true;
	}

	return new \Zend\View\Model\JsonModel(array(
	    'done' => $done,
	));
    }
    
    /**
     * Effacer une prime
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function eraseAction() {
	// layout
	$this->layout('layout/ajax');
	// post
	$post		 = $this->getRequest()
		->getPost();
	// table motif
	$prmTbl	 = new \RHX\Model\Table\PrimeTable($this->getAdapter());
	$nb		 = $prmTbl->erase($post->pk);

	return new \Zend\View\Model\JsonModel(array(
	    'done'	 => ($nb > 0) ? true : false,
	    'nb'	 => $nb,
	    'post'	 => $post,
	));
    }
    
    /**
     * Ajouter une prime
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function addAction() {
	// POST
	$post = $this->getRequest()
		->getPost();

	// Nouveau
	$prime = new \RHX\Model\Object\Prime($post);

	// Table  Séjour
	$prmTbl = new \RHX\Model\Table\PrimeTable($this->getAdapter());

	$id	 = 0;
	$err	 = null;

	try {
	    $id = $prmTbl->save($prime);
	}
	catch (\Exception $ex) {
	    $err = $ex;
	}

	return new \Zend\View\Model\JsonModel(array(
	    'done'	 => (is_null($err)) ? true : false,
	    'error'	 => $err,
	    'id'	 => $id,
	));
    }

}

